package com.baizan.daka.ui.activity

import android.content.Context
import android.content.Intent
import android.text.InputType
import android.view.View
import com.baizan.base.BaseActivity
import com.baizan.daka.AppUpdateManager
import com.baizan.daka.Constants
import com.baizan.daka.mvp.model.bean.LoginInfoBean
import com.baizan.daka.showToast
import com.baizan.daka.startActivity
import com.baizan.daka.ui.contract.LoginContract
import com.baizan.daka.ui.presenter.LoginPresenter
import com.baizan.daka.util.Preference
import com.baizan.daka.util.StringMatches
import com.dianzan.daka.R
import kotlinx.android.synthetic.main.act_login.*

class LoginActivity : BaseActivity<LoginPresenter>(), LoginContract.View {

    companion object {

        fun start(context: Context) {
            val intent = Intent(context, LoginActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
        }
    }

    override fun initPresenter(): LoginPresenter {
        return LoginPresenter(this)
    }

    override fun layoutId(): Int {
        return R.layout.act_login
    }

    override fun initData() {
        var accountCache: String by Preference(Constants.USER_ACCOUT, "")
        account_et.setText(accountCache)
        if (accountCache.isNullOrEmpty()) {
            account_et.setSelection(account_et.text.length)
        } else {
            password_et.requestFocus()
            password_et.setSelection(password_et.text.length)
        }
        password_et.isPressed = true
        eye_iv.isSelected = true
        submit_bt.setOnClickListener {
            var account = account_et.text
            if (account.isEmpty()) {
                showToast("请输入用户名/邮箱")
                return@setOnClickListener
            }
            var password = password_et.text
            if (password.isEmpty()) {
                showToast("请输入密码")
                return@setOnClickListener
            }
//            if(!StringMatches.matchesPassword(password.toString())){
//                showToast("请输入8-16位数字加字母的密码")
//                return@setOnClickListener
//            }
            presenter?.login(account.toString(), password.toString())
        }
        eye_iv.setOnClickListener {
            it.isSelected = !it.isSelected
            if (it.isSelected)
                password_et.inputType =
                    InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
            else
                password_et.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD

        }
        forget_password_tv.setOnClickListener {
            RegisterActivity.startFind(this)
        }
    }

    override fun initView() {
        //点击Home键后再次打开导致APP重启问题
        // 来源 https://blog.csdn.net/LVXIANGAN/article/details/82870762
        // 避免从桌面启动程序后，会重新实例化入口类的activity

        action_bar.setLeftShow(true)
        action_bar.setLeftShow(false)
        action_bar.setRightOnClickListener(View.OnClickListener {
            startActivity<RegisterActivity>()
        })

    }

    override fun start() {

    }

    override fun loginSuccess(loginInfo: LoginInfoBean, account: String) {
        var loginInfosave: LoginInfoBean by Preference(Constants.LOGIN_INFO_KEY, loginInfo)
        loginInfosave = loginInfo
        var accountCache: String by Preference(Constants.USER_ACCOUT, account)
        accountCache = account
        var isLogin: Boolean by Preference(Constants.ISLOGIN, true)
        isLogin = true;
        showToast("登录成功")
        startActivity<HomeActivity>()
        finish()
    }


}